﻿Public Class frmGetFiles

    Public FilenamesList As List(Of String)
    Public FilePathsList As List(Of String)
    Public filetypes As List(Of String)
    Public FilenameDisplay As List(Of String)
    Public path As String
    Dim mode As Integer = 0

    Private Sub frmGetFiles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        mode = 0
        Filelist.Items.Clear()
        'Dim var orderCounts =  from c in FilePathsLList select new {  GetFileName(c), OrderCount = c.Count() }
        'Dim var2 = From c In FilePathsLList Group c By key = GetFilename(c) Into Group Select filename = GetFilename(c), Number = c
        FilenameDisplay = New List(Of String)

        For Each filename As String In FilenamesList
            Dim FilenameCount As Integer = Aggregate fp In FilePathsList Where GetFilename(fp) = filename Into Count()
            Dim formatString As String = String.Format("{0, 5}    {1}", FilenameCount, filename)
            FilenameDisplay.Add(formatString)
        Next


        Filelist.Items.AddRange(FilenameDisplay.ToArray())
        Filelist.Sorted = True
        Button2.Enabled = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub



    Function GetExtension(path As String) As String
        Dim i As Integer = path.LastIndexOf(".")
        If i > 0 Then
            Return path.Substring(i)
        End If
        Return ""
    End Function

    Function GetFilename(path As String) As String
        'Remove trailing \
        If (path.Substring(path.Length - 1) = "\") Then
            path = path.Substring(0, (path.Length - 1))
        End If

        'Chop off after last \
        Dim i As Integer = path.LastIndexOf("\")
        If i > 0 Then
            Return path.Substring(i + 1)
        End If
        Return path
    End Function

    Private Sub Filelist_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Filelist.MouseDoubleClick
        If (Filelist.Items.Count = 0) Then Return
        If (Filelist.SelectedIndex = -1) Then Return
        Dim filename As String = Filelist.SelectedItem.ToString()
        If mode = 0 Then
            filename = filename.Substring(9)  '9 from: String.Format("{0, 5}    {1}", FilenameCount, filename)
            Dim var2 = From fpath As String In FilePathsList Where GetFilename(fpath) = filename Select fpath
            Dim FilepathListStr() As String = var2.ToArray()
            Filelist.Items.Clear()
            Filelist.Items.AddRange(FilepathListStr.ToArray())
            mode = 1
            Button2.Enabled = True
        Else
            Dim path As String = filename
            'Ref: http://support.microsoft.com/kb/152457
            path = "/e, /select, " + path
            Process.Start("Explorer.exe", path)
            Me.Close()
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If (mode = 1) Then
            Filelist.Items.Clear()
            Filelist.Items.AddRange(FilenameDisplay.ToArray())
            Filelist.Sorted = False
            Filelist.Sorted = True
            Button2.Enabled = False


            mode = 0
        Else

        End If
    End Sub
End Class